const path = require('path')
const { renderPost, renderIndex } = require('../../utils/index')

exports.showPost = async (req, res, next) => {
  try {
    // 解析获取真实的 md 文件路径
    const originPath = req.params[0]
    const pathObj = path.parse(originPath)
    const filePath = `${path.join('_posts', pathObj.dir, pathObj.name)}.md`
    res.send(renderPost(filePath))
  } catch (err) {
    next(err)
  }
}

exports.showHome = async (req, res, next) => {
  try {
    // 遍历生成文章列表
    res.send(renderIndex())
  } catch (err) {
    next(err)
  }
}
